/*
===========================================================================
This source file is part of DyLab (Dynamic Laboratory)
For the latest info, see http://dylab.googlecode.com

Copyright (c) 2006-2008 Lukas Krejci
(krejci.lukas@volny.cz)

This file is part of DyLab.

    DyLab is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    DyLab is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DyLab.  If not, see <http://www.gnu.org/licenses/>.
===========================================================================
*/

// ============================================================================================================================
#include <dylab/simobject/masspoints/MassPoint.h>

// ============================================================================================================================
// dylab::MassPoint::HistoryStates
// ============================================================================================================================
dylab::MassPoint::HistoryStates::HistoryStates(uint32_t _size, const State & _currState)
: mSize(_size)
{ 
	DYLAB_ASSERT(mSize > 0);
	DYLAB_ASSERT(mSize <= MAX_HISTORY_SIZE);
	for (uint32_t i = 0; i < mSize; i++)
	{
		State * s = new State(_currState);
		DYLAB_ASSERT(s != NULL);
		mStates[i] = s;
	}
}
//-----------------------------------------------------------------------------
dylab::MassPoint::HistoryStates::~HistoryStates()
{
	for (uint32_t i = 0; i < mSize; i++)
		delete mStates[i];
}
//-----------------------------------------------------------------------------
void dylab::MassPoint::HistoryStates::storeState(const State & _currState)
{
	for (int32_t i = mSize; i > 0; i--)
	{
		if (i-2 >= 0)
			*(mStates[i-1]) = *(mStates[i-2]);
		else
			*(mStates[i-1]) = _currState;
	}
}
//-----------------------------------------------------------------------------

// ============================================================================================================================
// dylab::MassPoint
// ============================================================================================================================
dylab::MassPoint::MassPoint()
{
}
//-----------------------------------------------------------------------------
dylab::MassPoint::MassPoint(id_t _id, const Vector3 & _pos , real_t _mass, BitFlagsT::data_t _flags)
: mId(_id), mMass(_mass), mFlags(_flags), mCurrState(_pos, Vector3::ZERO, Vector3::ZERO)
, mHistoryStates(NULL)
{
}
//-----------------------------------------------------------------------------
dylab::MassPoint::~MassPoint()
{
}
//-----------------------------------------------------------------------------
void dylab::MassPoint::reserveHistoryStates(uint32_t _noStates)
{
	if (mHistoryStates != NULL) 
	{
		if (mHistoryStates->getSize() < _noStates)
			delete mHistoryStates;
		else
			return;
	}
	mHistoryStates = new HistoryStates(_noStates, mCurrState);
	DYLAB_ASSERT(mHistoryStates != NULL);
}
//-----------------------------------------------------------------------------